/**
* @mxmlc -output=bin/assets/modules/strategy.swf -external-library-path+=lib/library.swc -debug=false -noplay
*/
package strategy 
{
	import flash.display.Bitmap;
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	import flash.utils.Dictionary;
	import interfaces.IOperate;
	import org.airmvc.View;
	import strategy.view.Castle;
	import utils.T;
	import vo.CastleVO;
	import vo.PlayerVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-02-28
	 */
	public class StrategyV extends View implements IOperate
	{
		
		public function StrategyV(){}
		
		override public function init():void 
		{			
			backMap = new Bitmap(App.asset.getBitmapData("png.strategy.map2"));
			traffic = new Shape();
			traffic.graphics.lineStyle(2, 0x000000, 1, false, LineScaleMode.NONE, CapsStyle.NONE, JointStyle.MITER);
			foreMap = new Bitmap(App.asset.getBitmapData("png.strategy.map1"));
			castlePanel = new Sprite();
			cursor = new Bitmap(App.asset.getBitmapData("png.strategy.cursor"));
			cursor.visible = false;
			
			addChild(backMap);
			addChild(traffic);
			addChild(foreMap);
			addChild(castlePanel);
			addChild(cursor);
			
			castleList = new Dictionary();
			config = T.db.getXml(Cnt.XML_CASTLE);
			var t:Dictionary = T.db.getTable(Cnt.VO_CASTLE);
			var pId:int;
			for each(var x:XML in config.castle)
			{
				var cid:int = int(x.@id);
				var c:Castle = new Castle(cid);
				pId = (t[cid] as CastleVO).playerID;
				c.update(pId);
				c.x = x.@x;
				c.y = x.@y;
				castlePanel.addChild(c);
				castleList[cid.toString()] = c;
				drawTraffic(x);
			}
			Lyr.scene.addChild(this);
		}
		
		override protected function addListeners():void 
		{
			receive(StrategyC.SHOW_OR_HIDE,handleShowOrHide);
			receive(StrategyC.SWITCH_MAP, handleSwitchMap);
			receive(StrategyC.ACTIVE_CASTLE, handleActive);
			receive(StrategyC.UNACTIVE_CASTLE, unActiveCastle);
			receive(StrategyC.UPDATE_CASTLE, updateCastle);
			receive(StrategyC.CHOOSE_CASTLE, chooseCastle);
			castlePanel.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function chooseCastle(castles:Array):void 
		{
			canChooseCastles = castles;
			index = _index;
			cursor.visible = true;
			Kyb.setOperater(this);
		}
		
		public function execute(keyCode:int):void
		{
			switch (keyCode) 
			{
				case Keyboard.A:
					index--;
				break;
				case Keyboard.W:
					index--;
				break;
				case Keyboard.D:
					index++;
				break;
				case Keyboard.S:
					index++;
				break;
				case Keyboard.SPACE:
					cursor.visible = false;
					clickCastle = castleList[canChooseCastles[_index]];
					send(StrategyC.CLICK_CASTLE, int(canChooseCastles[_index]));
				break;
			}
		}
		
		private function unActiveCastle():void 
		{
			if (curCastle)
			{
				curCastle.visible = true;
				curCastle.mouseEnabled = true;
				App.timer.clearTimer(activeCastle);
			}
		}
		
		private function updateCastle(castleId:int,playerId:int):void
		{
			castleList[String(castleId)].update(playerId);
		}
		
		private function handleActive(isActive:Boolean):void 
		{
			unActiveCastle();
			
			if (isActive)
			{
				cursor.visible = false;
				curCastle = clickCastle;
				curCastle.mouseEnabled = false;
				_index = canChooseCastles.indexOf(String(clickCastle.id));
				
				App.timer.doLoop(200,activeCastle);
			}
		}
		
		private function activeCastle():void 
		{
			curCastle.visible = !curCastle.visible;
		}
		
		private function onClick(e:MouseEvent):void 
		{
			clickCastle = e.target as Castle;
			if (clickCastle)
			{
				send(StrategyC.CLICK_CASTLE, clickCastle.id);
			}
		}
		
		private function handleSwitchMap():void 
		{
			backMap.visible = foreMap.visible;
			traffic.visible = foreMap.visible;
			foreMap.visible = !foreMap.visible;
		}

		private function handleShowOrHide(isShow:Boolean):void 
		{
			this.visible = isShow;
		}
		
		/**
		 * 画交通图路线
		 */
		private function drawTraffic(cx:XML):void
		{
			var id:String;
			var x1:int = int(cx.@x) + 8;
			var y1:int = int(cx.@y) + 6;
			//曲线的偏移量
			var dx:int = 7;
			var dy:int = 2;
			var conList:Array = String(cx.@conList).split(',');
			for each(id in conList)
			{
				var ox:XML = config.castle.(@id == id)[0];
				var x2:int = int(ox.@x) + 8;
				var y2:int = int(ox.@y) + 6;
				//从当前城池坐标趋向目标城池画曲线
				if (x1 < x2)
				{
					traffic.graphics.moveTo(x1, y1);
					
					//求出两坐标的直接方程
					var k:Number = (y1 - y2) / (x1 - x2);
					var b:Number = y1 - k * x1;
					var t:Number = x1;
					while (t < x2)
					{
						t += dx;
						var p:Point = new Point;
						if (t < x2)
						{
							//从直线两侧获得一些随机点
							p.x = t;
							p.y = Math.random() * 3 < 1?k * t + b + dy:k * t + b - dy;
						}
						else
						{
							p.x = x2;
							p.y = y2;
						}
						
						//连接这些点
						traffic.graphics.lineTo(p.x, p.y);
					}
				}
			}
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(value:int):void 
		{
			if (value < 0 )
				value = canChooseCastles.length - 1;
			else if (value >= canChooseCastles.length)
				value = 0;
				
			var c:Castle = castleList[canChooseCastles[value]];
			cursor.x = c.x;
			cursor.y = c.y + 6;
			_index = value;
		}
		
		/**交通图的背景地图*/
		private var backMap:Bitmap;
		/**交通图之上的地形地图*/
		private var foreMap:Bitmap;
		/**交通图路线*/
		private var traffic:Shape;
		/**城池列表*/
		private var castleList:Dictionary;
		/**城池容器*/
		private var castlePanel:Sprite;
		/**城池配置*/
		private var config:XML;
		/**当前点击的城池*/
		private var curCastle:Castle;
		/**被点击的城池*/
		private var clickCastle:Castle;
		/**光标*/
		private var cursor:Bitmap;
		/**可以选择的城池*/
		private var canChooseCastles:Array;
		/**当前选择的城池序号*/
		private var _index:int;
		
		
	}
	
}